const app = getApp()
// const user = wx.getStorageSync("user");
var user_token = wx.getStorageSync('user_token');
const base_url = app.globalData.base_url;

const date = new Date()
const years = []
const months = []
const days = []
const util = require('../../utils/util.js');
for (let i = 1900; i <= date.getFullYear(); i++) {
  years.push(i)
}

for (let i = 1; i <= 12; i++) {
  months.push(i)
}

for (let i = 1; i <= 31; i++) {
  days.push(i)
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sheng: ['京', '沪', '津', '渝', '浙', '苏', '冀', '晋', '蒙', '黑', '辽', '吉', '皖', '闽', '赣', '鲁', '豫', '鄂', '湘', '粤', '桂', '琼', '川', '贵', '云', '藏', '陕', '甘', '青', '宁', '新'],
    carinfo: {},
    sex: '1',
    pickerFlag: false,
    years: years,
    birthday: '',
    year: date.getFullYear(),
    months: months,
    month: date.getMonth() + 1,
    days: days,
    day: date.getDate(),
    value: [9999, 1, 1],
    formData: {},
    selectSex: [
      { name: '男', value: '1' },
      { name: '女', value: '0' },
    ],
    real_name: '',
    company: '',
    company_address: '',
    company_introduction: '',
    constellation: '',
    position: '',
    wx_number: '',
    index: 0,
  },
  onLoad: function (options) {
    wx.request({
      url: base_url + '/Users/getMyCarInfo',
      method: 'POST',
      data: {
        'user_token': JSON.parse(wx.getStorageSync("user")).user_token
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res)
        if (res.data.success == '1') {
          this.setData({
            carinfo: res.data.data,
            'formData.birthday': res.data.data.brithday
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          }),
            wx.navigateBack({
              delta: 1
            })
        }
      }
    })
  },
  confirm: function () {
    let data = this.data.formData;
    this.setData({ 'formData.user_token': JSON.parse(wx.getStorageSync("user")).user_token })
    wx.request({
      url: base_url + '/Users/setUserInfo',
      method: 'POST',
      data: data,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        if (res.data.success == '1') {
          wx.redirectTo({
            url: '../myInfo/index'
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }

      }
    })
  },

  listenerPickerSelected: function (e) {
    //改变index值，通过setData()方法重绘界面
    this.setData({
      index: e.detail.value,
      car_str: this.data.sheng[e.detail.value]
    });
  },
  selSex: function (e) {
    this.setData({ 'formData.sex': e.currentTarget.dataset.sex })
  },
  selBirth: function () {//打开时间选择器
    this.setData({ pickerFlag: !this.data.pickerFlag })
  },
  clear: function () {//关闭选择器
    this.setData({ pickerFlag: false })
  },
  saveBirth: function () {//提交生日
    this.setData({
      'formData.birthday': util.zero(this.data.year) + '-' + util.zero(this.data.month) + '-' + util.zero(this.data.day),
      pickerFlag: false
    })
  },
  getData(e) {
    let vtype = e.currentTarget.dataset.vtype;
    let label = 'formData.' + vtype;
    this.setData({
      [label]: e.detail.value
    })
  },
  bindChange: function (e) {//时间选择器
    const val = e.detail.value
    this.setData({
      year: this.data.years[val[0]],
      month: this.data.months[val[1]],
      day: this.data.days[val[2]]
    })
  },


})
